<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cart;
use Session;
class DingDanController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // dd(session('id'));
        if(empty(session('username'))){
            return redirect('/login');
            Cart::destroy();
        }

        $cart = Cart :: content();
        $total = Cart :: subtotal();
        $count = Cart :: count();
        $address = DB::table('addresses')
        ->where('addresses.uid','=',session('id'))
        ->get();
        // dd($address);
        return view('home.dingdan',['cart'=>$cart,'total'=>$total,'count'=>$count,'add'=>$address]);
    }

    /**
     *确认订单插入数据库
     * 
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        
        // 获取地址id
        $addressid = session('addressid');
        if(empty($addressid)){
            return back()->with('info','<script>alert("您还没有添加地址哦")</script>');
        }

        // dd($addressid);
        $cart = Cart :: content();
        $money = Cart :: subtotal();
        // dd($cart);
        $address = DB::table('addresses')
        ->where('addresses.uid','=',session('id'))
        ->get();
        if(empty($address)){
            return back()->with('info','<script>alert("您还没有收获地址哦")</script>');
        }

        // dd($address);

        $ordernum = time().rand(1000000,9999999);
        // dd($ordernum);
        // 插入订单表
        $id = DB::table('orders')->insertGetId(
           ['uid'=>session('id'),'money'=>$money,'createtime'=>time(),'ordernum'=>$ordernum,'addressid'=>$addressid]
        );
        session(['oid'=>$id]);
        //插入订单详情表
        foreach($cart as $v ){
           $res = DB::table('orderdetails')
            ->insert(['oid'=>$id,'gid'=>$v->id,'sid'=>$v->options->size,'count'=>$v->qty]);
            
        }
        Cart :: destroy();
        $res = DB::table('orderdetails')->get();
        // dd($res);
        return redirect('/dingdan/cashier');

    }
    /**
     * 将地址id存入到session中
     * [addressid description]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function addressid(Request $request)
    {
        // echo $request->addressid;
        if(empty($request->addressid)){
            return 1;  
        } 
        session(['addressid'=>$request->addressid]);
    }

    /**
     * 收银页面
     * [cashier description]
     * @return [type] [description]
     */
    public function cashier()
    {
        // dd(session('oid'));
       $res = DB::table('orderdetails')
            ->where('orderdetails.oid','=',Session::get('oid'))
            ->select()
            ->get();

        // $res1 = DB::table('addresses')
        //     ->join('orders','orders.id','=',session('oid'))
        //     ->where('addresses.id','=','orders.addressid')
        //     ->select()
        //     ->get();
        $res1 = DB::table('orders')->where('id','=',session('oid'))->select()->first();
        $address = DB::table('addresses')->where('id','=',$res1->addressid)->select()->first();
    ;
        // foreach($res as $k){

            $ress = DB::table('orderdetails')
                ->join('orders','orderdetails.oid','=','orders.id')
                ->join('goods','orderdetails.gid','=','goods.id')
                ->join('shops','shops.id','=','orderdetails.sid')
                ->where('orderdetails.oid','=',session('oid'))
                ->select('shops.*','goods.*')
                ->get();
            // dd($ress);
            // $orders[] = $ress;
            // }
      
        // dd($orders);
        return view('/home/UserCenter/cashier',['orders'=>$ress,'address'=>$address]);
    }

    public function alipay()
    {
        
        $res = DB::table('orders')->where('id','=',session('oid'))->Update(['orderstatus'=>1]);
        if($res){
            return redirect('/UserCenter/center');
        }
    }



    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
